// 设置文件
import {
  Message
} from 'element-ui'
import setting from '@/setting.js'
import api from '@/api'
import util from '@/libs/util.js'
import {commit} from "lodash/seq";

export default {
  namespaced: true,
  state: {
    // 顶栏菜单
    header: [],
    // 侧栏菜单
    aside: [],
    // 侧边栏收缩
    asideCollapse: setting.menu.asideCollapse,
    // 侧边栏折叠动画
    asideTransition: setting.menu.asideTransition
  },
  getters: {
    // 按钮是否显示
    showButton: state => (path, btnName) => {
      // 状态无效
      if(!state.aside){
        return false;
      }
      // 递归函数
      let func = function(path, btnName, arr){
        for(let i = 0; i < arr.length; ++i){
          if(arr[i].path === path) {
            if(!arr[i].buttons) {
              return false;
            }
            for(let j = 0; j < arr[i].buttons.length; ++j){
              if(arr[i].buttons[j].title === btnName){
                return true;
              }
            }
            return false;
          }
          if(!arr[i].children){
            continue;
          }
          if(func(path, btnName, arr[i].children)){
            return true;
          }
        }
        return false;
      }
      return func(path, btnName, state.aside);
    },

    aside: state=>state.aside
    },
  actions: {
    /**
     * 设置侧边栏展开或者收缩
     * @param {Object} context
     * @param {Boolean} collapse is collapse
     */
    async asideCollapseSet({state, dispatch}, collapse) {
      // store 赋值
      state.asideCollapse = collapse
      // 持久化
      // await dispatch('ddAdmin/db/set', {
      //   dbName: 'sys',
      //   path: 'menu.asideCollapse',
      //   value: state.asideCollapse,
      //   user: true
      // }, {root: true})
    },
    /**
     * 切换侧边栏展开和收缩
     * @param {Object} context
     */
    async asideCollapseToggle({state, dispatch}) {
      // store 赋值
      state.asideCollapse = !state.asideCollapse
      // 持久化
      await dispatch('ddAdmin/db/set', {
        dbName: 'sys',
        path: 'menu.asideCollapse',
        value: state.asideCollapse,
        user: true
      }, {root: true})
    },
    /**
     * 设置侧边栏折叠动画
     * @param {Object} context
     * @param {Boolean} transition is transition
     */
    async asideTransitionSet({state, dispatch}, transition) {
      // store 赋值
      state.asideTransition = transition
      // 持久化
      await dispatch('ddAdmin/db/set', {
        dbName: 'sys',
        path: 'menu.asideTransition',
        value: state.asideTransition,
        user: true
      }, {root: true})
    },
    /**
     * 切换侧边栏折叠动画
     * @param {Object} context
     */
    async asideTransitionToggle({state, dispatch}) {
      // store 赋值
      state.asideTransition = !state.asideTransition
      // 持久化
      await dispatch('ddAdmin/db/set', {
        dbName: 'sys',
        path: 'menu.asideTransition',
        value: state.asideTransition,
        user: true
      }, {root: true})
    },
    /**
     * 持久化数据加载侧边栏设置
     * @param {Object} context
     */
    async asideLoad({state, dispatch}) {
      // store 赋值
      const menu = await dispatch('ddAdmin/db/get', {
        dbName: 'sys',
        path: 'menu',
        defaultValue: setting.menu,
        user: true
      }, {root: true})
      state.asideCollapse = menu.asideCollapse !== undefined ? menu.asideCollapse : setting.menu.asideCollapse
      state.asideTransition = menu.asideTransition !== undefined ? menu.asideTransition : setting.menu.asideTransition
    },
    /**
     * @description 获取用户菜单
     * @param {*} param0
     */
    async getUserMenus({ state, dispatch, commit }) {
      return await new Promise((resolve, reject) => {
        // 查询用户菜单
        api.menu.getUserMenus().then(async (res) => {
          // 菜单不为空
          if(res && res.value && res.value.length > 0){
            // 排序函数
            let sort = function(arrs) {
              arrs.sort((a, b) => a.number - b.number);
              arrs.forEach(item => {
                if(item.children && item.children.length > 0){
                  sort(item.children);
                }
              });
            }
            // 菜单排序
            sort(res.value);
            state.aside = res.value;
            // 更新菜单
            commit('asideSet', res.value);
            resolve(res.value);
          } else {
            Message({ message: '用户无菜单访问权限！', type: 'warning' })
            state.aside = [];
            reject(res)
          }
        }).catch((error) => {

        })
      })
    }
  },
  mutations: {
    /**
     * @description 设置顶栏菜单
     * @param {Object} state state
     * @param {Array} menu menu setting
     */
    headerSet(state, menu) {
      // store 赋值
      state.header = menu
    },
    /**
     * @description 设置侧边栏菜单
     * @param {Object} state state
     * @param {Array} menu menu setting
     */
    asideSet(state, menu) {
      // store 赋值
      state.aside = menu
      state.aside.sort((a, b) => a.number - b.number);
    },
    /**
     * @description 动态追加菜单
     * @param {*} state
     * @param {*} menu
     */
    addAside(state, menu) {
      menu.forEach(item => {
        var exist = false;
        for(var i = 0; i < state.aside.length; ++i){
          if(state.aside[i].name === item.name){
            exist = true;
            break;
          }
        }
        if(!exist){
          state.aside.push(item);
        }
      });
      state.aside.sort((a, b) => a.number - b.number);
    }
  }
}
